package com.lirh.data.mongodbdemo.contorller;

import com.lirh.data.mongodbdemo.dao.MongoTestDao;
import com.lirh.data.mongodbdemo.model.MongoTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MongoTestController {
    @Autowired
    private MongoTestDao mtdao;
    private  Integer a=121;
//    新增数据
    @RequestMapping("/test1")
    public void saveTest() throws Exception{
        System.out.println("123");
        MongoTest mgtest=new MongoTest();
        mgtest.setId(a++);
        mgtest.setAge(33);
        mgtest.setName("lirh");
        mtdao.saveTest(mgtest);
    }
//    查找数据
    @GetMapping("/test2")
    public MongoTest findTestByName(){

        MongoTest mgtest=mtdao.findTestByName("lirh");
        return mgtest;
    }
//    修改数据
    @GetMapping("/test3")
    public void updateTest(){
        MongoTest mgtest = new MongoTest();
        mgtest.setId(21);
        mgtest.setAge(123456778);
        mgtest.setName("lirh_new");
        mtdao.updateTest(mgtest);
    }
//    删除数据
    @GetMapping("/test4/{id}")
    public void deleteTestById(@PathVariable("id")Integer id){
        mtdao.deleteTestById(id);
    }
    @GetMapping("/hello")
    public String  hello() throws Exception{
        return "hello";
    }
}
